//
//  SetViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "SetViewController.h"

#import "MineAPIManager.h"
@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)NSArray *titleArray;

@property(nonatomic ,strong)NSDictionary *dic;
@end

@implementation SetViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lClearColor] backgroundColor:lClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = rgba(245, 245, 245, 1);
    [self checkVersion];
}
- (void)checkVersion
{
    [MineAPIManager CheckVersionWithParam:@{@"platform":@"ios",@"current_version":[MethodTool appVersion]} result:^(NSDictionary * _Nonnull dic) {
       
        if ([dic[@"data"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary *adic =  dic[@"data"];
            if(adic.allValues.count != 0)
            {
                self.dic = adic;
            }
        }
        [self.tableView reloadData];
    }];
}
- (void)logoutBtnAction
{
    [lApp goLogin];
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    for (UIView *view in cell.contentView.subviews) {
        if (view.tag == 123||view.tag == 123) {
            [view removeFromSuperview];
        }
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.font = MFontWithSize(16);
    cell.textLabel.textColor = rgba(51, 51, 51, 1);
    cell.accessoryView = [[UIImageView alloc] initWithImage:IMAGENAMED(@"ic_mine_arrow_black")];
    if ([cell.textLabel.text isEqualToString: @"版本更新"]) {
        
        UIView *pot = [[UIView alloc] initWithFrame:CGRectMake(lScreenWidth - 46,27, 8, 8)];
        pot.tag = 124;
        NSString *vser = self.dic[@"version"];
        pot.hidden = vser.length == 0;
        pot.backgroundColor = rgba(250, 25, 44, 1);
        [pot setAllCornersWithCornerRadius:4];
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 122, 62)];
        lab.tag = 123;
        lab.text = [NSString stringWithFormat:@"V%@",[MethodTool appVersion]];
        lab.textColor = rgba(142, 142, 142, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        double w = [MethodTool getTextWidthWithStr:lab.text font:lab.font ViewHeight:lab.height];
       
        lab.width = w;
        lab.x = pot.x - 10 - w;
        if (vser.length == 0) {
            lab.x = lScreenWidth - 15 - w;
            cell.accessoryView = nil;
        }
        [cell.contentView addSubview:lab];
        [cell.contentView addSubview:pot];
       
    }
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 62;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.titleArray[indexPath.row] isEqualToString:@"个人信息"]) {
        [self pushToViewControllerWithClassName:@"PersonInformationVC" animated:YES];
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"版本更新"]) {
        [MethodTool checkVersionUpdateWithInfo:self.dic];
    }
    
    if ([self.titleArray[indexPath.row] isEqualToString:@"注销账号"]) {
        [self pushToViewControllerWithClassName:@"AccountCancellationVC" animated:YES];
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"关于学而时习"]) {
        [self pushToViewControllerWithClassName:@"AboutLearnController" animated:YES];
    }
    if ([self.titleArray[indexPath.row] isEqualToString:@"撤销同意说明"]) {
        [self pushToViewControllerWithClassName:@"AccountCancellationExplainVC" animated:YES];
    }
    
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, lTopBarHeight, lScreenWidth, lScreenHeight - lTopBarHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lClearColor];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_tableView setTableFooterView:self.tableFootorView];
    }
    
    return _tableView;
}
-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 146)];
        _tableFootorView.backgroundColor = lClearColor;
        UIButton *logoutBtn = [UIButton new];
        logoutBtn.frame =CGRectMake(0, 0, lScreenWidth - 20, 45);
       
        logoutBtn.selected =NO;
        [logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [logoutBtn setTitleColor:lWhiteColor forState:UIControlStateNormal];
        [logoutBtn.titleLabel setFont:MFontWithSize(18)];
        [logoutBtn addTarget:self action:@selector(logoutBtnAction) forControlEvents:UIControlEventTouchUpInside];
        logoutBtn.backgroundColor = lClearColor;
        UIView *btnView =[[UIView alloc] initWithFrame:CGRectMake((lScreenWidth - logoutBtn.width)/2.0, 60, logoutBtn.width, logoutBtn.height)];
        [btnView setAllCornersWithCornerRadius:logoutBtn.height/2.0];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,btnView.width,btnView.height);
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [btnView.layer addSublayer:gl];
        [btnView addSubview:logoutBtn];
        [_tableFootorView addSubview:btnView];
        [_tableFootorView setHeight:btnView.bottom];
    }
    
    return _tableFootorView;
    
}

- (NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@"个人信息",@"注销账号",@"版本更新",@"关于学而时习",@"撤销同意说明"];
    }
    return _titleArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
